/**
 *Copyright (c) 2024 watereyes
 * safetymanagement is licensed under Mulan PubL v2.
 * You can use this software according to the terms and conditions of the Mulan
 * PubL v2.
 * You may obtain a copy of Mulan PubL v2 at:
 *          http://license.coscl.org.cn/MulanPubL-2.0
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PubL v2 for more details.
 */
package com.koron.scene.controller;

import com.koron.common.StaffAttribute;
import com.koron.common.bean.StaffBean;
import com.koron.common.permission.SPIAccountAnno;
import com.koron.scene.service.api.ProjectFileService;
import com.koron.scene.utils.BusinessCode;
import com.koron.system.annotation.Log;
import com.koron.system.discrete.BusinessManipulation;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.swan.bean.MessageBean;
import springfox.documentation.annotations.ApiIgnore;

@Api(tags = "ProjectFile", description = "项目文件管理")
@RestController
@RequestMapping("/projectFile")
public class ProjectFileController {

    @Autowired
    private ProjectFileService projectFileService;

    @RequestMapping(value = "/addOrUpdate.htm", method = RequestMethod.POST)
    @ApiOperation(value = "项目相关方-保存或修改文件")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "projectFileId", value = "项目文件id", dataType = "String"),
            @ApiImplicitParam(name = "projectId", value = "项目id", dataType = "String"),
            @ApiImplicitParam(name = "enterpriseId", value = "企业id", dataType = "String"),
            @ApiImplicitParam(name = "sourceType", value = "文件类型", dataType = "String"),
            @ApiImplicitParam(name = "fileName", value = "文件名称", dataType = "String"),
            @ApiImplicitParam(name = "keepPathListStr", value = "保留文件path集合字符串" , dataType = "String"),
            @ApiImplicitParam(name = "deletePathListStr", value = "删除文件path集合字符串" , dataType = "String"),
            @ApiImplicitParam(name = "files", value = "项目材料", dataType = "file"),
    })
    @Log(title = "保存文件", businessModule = "项目文件管理",  businessManipulation = BusinessManipulation.INSERT)
    public MessageBean<?> addOrUpdate(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user,
                                      @RequestBody MultipartFile[] files, String projectFileId, String projectId, String enterpriseId, String sourceType,
                                      String fileName, String keepPathListStr, String deletePathListStr,
                                      String keepPrePathListStr, String deletePrePathListStr) {
        if (user == null) {
            MessageBean msg = new MessageBean();
            msg.setCode(BusinessCode.NO_LOGIN.getCode());
            msg.setDescription(BusinessCode.NO_LOGIN.getDescription());
            return msg;
        }
        return projectFileService.addOrUpdate(user, files, projectFileId, projectId, enterpriseId, sourceType,
                fileName, keepPathListStr, deletePathListStr, keepPrePathListStr, deletePrePathListStr);
    }

    @RequestMapping(value = "/delete.htm", method = RequestMethod.POST)
    @ApiOperation(value = "项目相关方-删除文件")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "projectFileId", value = "项目文件id", dataType = "String"),
    })
    @Log(title = "删除文件",businessModule = "项目文件管理", businessManipulation = BusinessManipulation.INSERT)
    public MessageBean<?> delete(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user, String projectFileId) {
        if (user == null) {
            MessageBean msg = new MessageBean();
            msg.setCode(BusinessCode.NO_LOGIN.getCode());
            msg.setDescription(BusinessCode.NO_LOGIN.getDescription());
            return msg;
        }
        return projectFileService.delete(user, projectFileId);
    }

    @RequestMapping(value = "/list.htm", method = RequestMethod.GET)
    @ApiOperation(value = "获取教育培训信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "fileName", value = "文件名称", dataType = "String"),
            @ApiImplicitParam(name = "sourceType", value = "文件类型", dataType = "String"),
            @ApiImplicitParam(name = "projectId", value = "项目id", dataType = "String"),
            @ApiImplicitParam(name = "enterpriseId", value = "企业id", dataType = "String"),
            @ApiImplicitParam(name = "page", value = "当前页" , dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数" , dataType = "Integer", paramType = "query")
    })
    public MessageBean<?> list(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user,
                               String fileName, String sourceType, String projectId, String enterpriseId,
                               Integer page, Integer pageSize) {
        if (user == null) {
            MessageBean msg = new MessageBean();
            msg.setCode(BusinessCode.NO_LOGIN.getCode());
            msg.setDescription(BusinessCode.NO_LOGIN.getDescription());
            return msg;
        }
        return projectFileService.list(user, fileName, sourceType, projectId, enterpriseId, page, pageSize);
    }
}
